package com.jiaoyou.baojian.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.activity.UserInfoActivity;
import com.jiaoyou.baojian.entity.Contribution;
import com.jiaoyou.baojian.entity.UserInfo;
import com.jiaoyou.baojian.utils.GlideCircleTransform;
import com.jiaoyou.baojian.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DemonZYM on 17/4/13.
 */

public class RankAdapter extends BaseMultiItemQuickAdapter<Contribution, BaseViewHolder> {
    private static List<Integer> icon_list = new ArrayList<>();
    static{
        icon_list.add(R.drawable.icon_no1);
        icon_list.add(R.drawable.icon_no2);
        icon_list.add(R.drawable.icon_no3);
        icon_list.add(R.drawable.icon_no4);
        icon_list.add(R.drawable.icon_no5);
        icon_list.add(R.drawable.icon_no6);
        icon_list.add(R.drawable.icon_no7);
        icon_list.add(R.drawable.icon_no8);
        icon_list.add(R.drawable.icon_no9);
        icon_list.add(R.drawable.icon_no10);
    }

    private int type;   //1 魅惑榜  2土豪榜
    private List<UserInfo> userInfos;
    public RankAdapter(List<Contribution> contributionList, int t) {
        super(contributionList);
        addItemType(Contribution.TYPE_TOP, R.layout.item_rank_top);
        addItemType(Contribution.TYPE_NORMAL, R.layout.item_rank);

        type = t;
    }

    public void setUserInfoList(List<UserInfo> list){
        userInfos = list;
    }
    public List<UserInfo> getUserInfoList(){
        return userInfos;
    }

    private boolean needTop = true;
    public void notNeedTop(){
        needTop = false;
    }

    @Override
    protected void convert(BaseViewHolder helper, final Contribution item) {
        LinearLayout layout = helper.getView(R.id.item_rank);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int uid = 0;
                if(type == 2)
                    uid = item.getFromUserId();
                else if(type == 1)
                    uid = item.getToUserId();
                UserInfoActivity.start(mContext, uid);
            }
        });
        int position = helper.getAdapterPosition();
        UserInfo userInfo = userInfos.get(position);
        if(position == 0 && needTop)
            item.isTop();
        helper.setText(R.id.name, userInfo.nickname + "，" + Utils.getAge(userInfo.birthday));
        Utils.showCircleImage(mContext, userInfo.avatar, (ImageView) helper.getView(R.id.header));
        int icon;
        if(helper.getLayoutPosition() >= icon_list.size()){
            icon = icon_list.get(9);
        } else {
            icon = icon_list.get(helper.getLayoutPosition());
        }
        Glide.with(mContext)
                .load(icon)
                .crossFade()
                .into((ImageView) helper.getView(R.id.icon));
        if(type == 1){
            helper.setText(R.id.text, "魅惑值");
            helper.setText(R.id.data, item.getAttractiveness() + "");
        } else if(type == 2){
            helper.setText(R.id.text, "土豪值");
            helper.setText(R.id.data, item.getContribution() + "");
        }
    }
}
